package icu.qx.pro.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.domain.SysUser;
import icu.qx.common.constant.TableNameEnum;
import icu.qx.common.utils.CodeUtils;
import icu.qx.pro.domain.ProWorkTask;
import icu.qx.pro.feign.SysUserFeign;
import icu.qx.pro.service.IProWorkTaskService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.pro.domain.ProWorkReport;
import icu.qx.pro.service.IProWorkReportService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 报工单Controller
 *
 * @author yong
 * @date 2024-06-30
 */
@RestController
@RequestMapping("/pro/proWorkReport")
public class ProWorkReportController extends BaseController {
    @Autowired
    private IProWorkReportService proWorkReportService;

    @Autowired
    private SysUserFeign sysUserFeign;

    @Autowired
    private CodeUtils codeUtils;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询报工单列表
     */
    @RequiresPermissions("pro:workReport:list")
    @GetMapping("/list")
    public TableDataInfo list(ProWorkReport workReport) {
        startPage();
        List<ProWorkReport> list = proWorkReportService.selectProWorkReportList(workReport);
        return getDataTable(list);
    }

    /**
     * 导出报工单列表
     */
    @RequiresPermissions("pro:workReport:export")
    @Log(title = "报工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProWorkReport workReport) {
        List<ProWorkReport> list = proWorkReportService.selectProWorkReportList(workReport);
        ExcelUtil<ProWorkReport> util = new ExcelUtil<ProWorkReport>(ProWorkReport.class);
        util.exportExcel(response, list, "报工单数据");
    }

    /**
     * 获取报工单详细信息
     */
    @RequiresPermissions("pro:workReport:query")
    @GetMapping(value = "/{reportCode}")
    public AjaxResult getInfo(@PathVariable("reportCode") String reportCode) {
        ProWorkReport workReport = proWorkReportService.selectProWorkReportByReportCode(reportCode);
        R<SysUser> username = sysUserFeign.username(workReport.getReportPeoCode());
        R<SysUser> username1 = sysUserFeign.username(Long.parseLong(workReport.getReportWManager()));
        R<SysUser> username2 = sysUserFeign.username(Long.parseLong(workReport.getReportWManager()));
        workReport.setReportPeoName(username.getData().getUserName());
        workReport.setReportQcPeoName(username2.getData().getUserName());
        workReport.setReportWMangerName(username1.getData().getUserName());
        return success(workReport);
    }

    /**
     * 新增报工单 报工
     */
    @RequiresPermissions("pro:workReport:add")
    @Log(title = "报工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProWorkReport workReport) {
        workReport.setReportCode(codeUtils.generateCode(TableNameEnum.PRO_WORK_REPORT.getTableName()));
        ProWorkTask proWorkTask = new ProWorkTask();
        proWorkTask.setWtaskCode(workReport.getReportTCode());
        proWorkTask.setWtaskStatus("3");
        rabbitTemplate.convertAndSend("proStatusConsumer.exchange", "changeWorkTask", proWorkTask);
        return toAjax(proWorkReportService.insertProWorkReport(workReport));
    }

    /**
     * 修改报工单 质检
     */
    @RequiresPermissions("pro:workReport:edit")
    @Log(title = "报工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProWorkReport workReport) {
        ProWorkTask proWorkTask = new ProWorkTask();
        proWorkTask.setWtaskCode(workReport.getReportTCode());
        if (("1").equals(workReport.getReportResult())) {
            proWorkTask.setWtaskStatus("4");
        } else {
            proWorkTask.setWtaskStatus("5");
        }
        rabbitTemplate.convertAndSend("proStatusConsumer.exchange", "changeWorkTask", proWorkTask);
        return toAjax(proWorkReportService.updateProWorkReport(workReport));
    }

    /**
     * 结算工资
     */
    @RequiresPermissions("pro:workReport:edit")
    @Log(title = "报工单", businessType = BusinessType.UPDATE)
    @PutMapping("/calcSal")
    public AjaxResult calcSal(@RequestBody ProWorkReport workReport) {
        workReport.setReportQc("2");
        return toAjax(proWorkReportService.calcSal(workReport));
    }

    /**
     * 删除报工单
     */
    @RequiresPermissions("pro:workReport:remove")
    @Log(title = "报工单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reportCodes}")
    public AjaxResult remove(@PathVariable String[] reportCodes) {
        return toAjax(proWorkReportService.deleteProWorkReportByReportCodes(reportCodes));
    }
}
